package com.jming.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jming.yygh.cmn.client.DictFeignClient;
import com.jming.yygh.enums.DictEnum;
import com.jming.yygh.model.user.Patient;
import com.jming.yygh.user.mapper.PatientMapper;
import com.jming.yygh.user.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jming
 * @version 0.0.1
 * @date 2021/05/12
 */
@Service
public class PatientServiceImpl
        extends ServiceImpl<PatientMapper, Patient>
        implements PatientService {

    @Resource
    DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllUserId(Long userId) {

        // 根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);

        // 通过过程调用，得到编码对应具体内容，查询数据字典表类容
        patientList.stream().forEach(item -> {
            // 其他参数的封装
            this.packPatient(item);
        });

        return patientList;
    }

    @Override
    public Patient getPatientId(Long id) {

        Patient patient = baseMapper.selectById(id);
        return this.packPatient(patient);

    }

    /**
     * 根据patient对象获取其他参数封装
     * @param patient
     * @Author jming
     */
    private Patient packPatient(Patient patient) {

        // 根据证件编码 获取证件类型的具体值
        // 联系人的证件类型
        String certificatesType =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),
                        patient.getCertificatesType());

        // 省
        String provinceCode = dictFeignClient.getName(patient.getProvinceCode());
        // 市
        String cityCode = dictFeignClient.getName(patient.getCityCode());
        // 区
        String districtCode = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesType", certificatesType);
        patient.getParam().put("provinceCode", provinceCode);
        patient.getParam().put("cityCode", cityCode);
        patient.getParam().put("districtCode", districtCode);

        return patient;
    }
}
